﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GosWeb.Models.Db;

namespace GosWeb.Controllers
{

    public class MedicalRecordController : Controller
    {

        GosDBContext db = GosDBContext.GetInstance();

        //public ActionResult Index()
        //{
        //    var genes = from m in db.MedicalRecords select m;
        //    return View(genes.ToList());
        //}

        public ActionResult Create(string id)
        {
            MedicalRecord gr = new MedicalRecord() { AnID = id, MedicalDate=DateTime.Now };
            return View(gr);
        }

        [HttpPost]
        public ActionResult Create(MedicalRecord newRec)
        {       
            newRec.IllCode = newRec.IllCode.Trim().ToUpper();
            if (ModelState.IsValid)
            {
                db.MedicalRecords.Add(newRec);
                db.SaveChanges();
                return RedirectToAction("Details", "Biont", new { id = newRec.AnID });
            }
            else
            {
                return View(newRec);
            }
        }

        public ActionResult Details(Int64 id)
        {
            MedicalRecord gr = db.MedicalRecords.Find(id);
            if (gr == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                //Location originalValues = db.Entry(biont).ComplexProperty(u => u.Address).OriginalValue;    
                return View("Details", gr);
            }
        }

        public ActionResult Edit(Int64 id)
        {
            MedicalRecord gr = db.MedicalRecords.Find(id);
            if (gr == null)
                return null;
            return View(gr);
        }


        [HttpPost]
        public ActionResult Edit(MedicalRecord model)
        {
            try
            {
                var gr = db.MedicalRecords.Find(model.MrID);

                UpdateModel(gr);
                db.SaveChanges();
                return RedirectToAction("Details", "Biont", new { id = gr.AnID });
            }
            catch (Exception)
            {
                ModelState.AddModelError("", "Edit Failure, see inner exception");
            }

            return View(model);
        }

        public ActionResult Delete(Int64 id)
        {
            var gr = db.MedicalRecords.Find(id);
            if (gr == null)
                return null;
            return View(gr);
        }

        [HttpPost]
        public RedirectToRouteResult Delete(Int64 id, FormCollection collection)
        {
            MedicalRecord gr = db.MedicalRecords.Find(id);
            if (gr != null)
            {
                string anid = gr.AnID;
                db.MedicalRecords.Remove(gr);
                db.SaveChanges();

                return RedirectToAction("Details", "Biont", new { id = anid });
            }
            else
            {
                return null;
            }
        }

        public ActionResult GetRecords(string id)
        {
            var bionts = (from m in db.MedicalRecords where (m.AnID == id) select m).OrderByDescending(m => m.MedicalDate);
            //bionts.OrderByDescending();
            return View("GetRecords", bionts.ToList());
        }

        public ActionResult Query(string ill, DateTime bDate, DateTime eDate)
        {
            ill = ill.Trim();
            eDate = eDate.AddDays(1);
            var bionts = (from m in db.MedicalRecords where (m.IllCode == ill && m.MedicalDate<eDate && m.MedicalDate>bDate) select m).OrderByDescending(m => m.MedicalDate);
            return  View( bionts);
        }

    }
}
